package com.huaxin.common.Deprecated;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 */

public  abstract class  BaseRecyclerViewAdapter<VH extends BaseRecyclerViewHolder> extends RecyclerView.Adapter<VH> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;
    private static final int TYPE_FOOTER = 2;
    private final Context context;
    private  LayoutInflater mInflater;

    private View mHeaderView;
    private View mFooterView;
    private List dataList;

    public Object getDataAtPosition(int position) {
        return dataList==null||dataList.size()<=position?null:dataList.get(position);
    }

    public BaseRecyclerViewAdapter(Context context) {
        this.context =  context;
        this.mInflater = LayoutInflater.from(context);
    }
    public BaseRecyclerViewAdapter(Context context,List datas) {
        this.context =  context;
        this.mInflater = LayoutInflater.from(context);
        this.dataList = datas;
    }
    public Context getContext() {
        return context;
    }
    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public void setDataList(List dataList) {
        this.dataList = dataList;
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        if(isShowFooterView()){
            notifyItemInserted(getItemCount()-1);
        }
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    public View getFooterView() {
        return mFooterView;
    }


    @Override
    public final VH onCreateViewHolder(ViewGroup parent, int viewType) {
        if (checkHeaderView(viewType)) {
            return createViewHolder(getHeaderView(),false);
        }
        if (checkFooterView(viewType)) {
            return createViewHolder(getFooterView(),false);
        }
        return createViewHolder(mInflater.inflate(getContentResourceId(), parent, false),true);
    }

    public abstract VH createViewHolder(View view, boolean isBind);
    public abstract int getContentResourceId();


    @Override
    public final void onBindViewHolder(VH holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        if (getItemViewType(position) == TYPE_FOOTER) return;
        holder.update(getRealPosition(holder));
    }


    @Override
    public int getItemViewType(int position) {
        if (isShowHeaderView() && position == 0) return TYPE_HEADER;
        if (isShowFooterView() && position == getItemCount() - 1) return TYPE_FOOTER;
        return TYPE_NORMAL;
    }

    @Override
    public int  getItemCount() {
        int size = dataList ==null?0: dataList.size();
        if (mHeaderView != null) {
            size++;
        }
        if (isShowFooterView()) {
            size++;
        }
        return size;
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    public boolean checkHeaderView(int viewType) {
        return mHeaderView != null && viewType == TYPE_HEADER;
    }

    public boolean checkFooterView(int viewType) {
        return mFooterView != null && viewType == TYPE_FOOTER;
    }

    private boolean isShowFooterView(){
        return mFooterView != null&&dataList!=null&&dataList.size()>0;
    }

    private boolean isShowHeaderView(){
        return mHeaderView != null;
    }

}


